package com.zxl.grocery.modular.crawler.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zxl.grocery.core.base.controller.BaseController;
import com.zxl.grocery.core.common.exception.BizExceptionEnum;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.reqres.response.ResponseData;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.kernel.model.exception.ServiceException;
import com.zxl.grocery.modular.crawler.entity.CrawlerSources;
import com.zxl.grocery.modular.crawler.model.CrawlerSourcesDto;
import com.zxl.grocery.modular.crawler.service.CrawlerSourcesService;
import com.zxl.grocery.modular.crawler.warpper.CrawlerMgrWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author zxl
 * @Date 2018-07-27 15:44:27
 */
@Controller
@RequestMapping("/crawlerMgr")
public class CrawlerMgrController extends BaseController {

    private static String PREFIX = "/crawler/crawlerMgr/";

    @Autowired
    private CrawlerSourcesService crawlerSourcesService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "crawlerMgr.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/crawlerMgr_add")
    public String crawlerMgrAdd() {
        return PREFIX + "crawlerMgr_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/crawlerMgr_update")
    public String crawlerMgrUpdate(@RequestParam Integer id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
//        CrawlerSources crawlerSources = crawlerSourcesService.selectById(id);
//        LogObjectHolder.me().set(crawlerSources);
        return PREFIX + "crawlerMgr_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "source", required = false) String source,
                       @RequestParam(value = "sourceUrl", required = false) String sourceUrl) {
        List<Map<String, Object>> result = crawlerSourcesService.getCrawlerSourceList(source, sourceUrl);
        return new CrawlerMgrWarpper(result).wrap();
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CrawlerSources crawlerSources) {
        this.crawlerSourcesService.addCrawlerSources(crawlerSources);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam Integer crawlerMgrId) {
        this.crawlerSourcesService.delCrawlerSourcesById(crawlerMgrId);
        return SUCCESS_TIP;
    }


    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CrawlerSourcesDto crawlerSourcesDto) {
        this.crawlerSourcesService.editCrawlerSources(crawlerSourcesDto);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{crawlerMgrId}")
    @ResponseBody
    public ResponseData detail(@PathVariable("crawlerMgrId") Integer crawlerMgrId) {
        if (ToolUtil.isEmpty(crawlerMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        CrawlerSources crawlerSources = this.crawlerSourcesService.getById(crawlerMgrId);
        LogObjectHolder.me().set(crawlerSources);
        Map<String, Object> crawlerSourcesMap = BeanUtil.beanToMap(crawlerSources);
        return ResponseData.success(crawlerSourcesMap);
    }
}
